$(document).ready(
				function() {
					
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					

					$("#roleDetailDiaglog").on('hide.bs.modal', function() {
						$("#roleList option").remove();
						$("#selectList option").remove();
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#userInfor").attr("method","post");
										if($("#selectList").find("option").length>0){
											
											$("#selectList option").attr("selected","selected");
											 $("#userInfor").attr("action","save.do");
											
											$("#userInfor").submit();
											}else{
												alert("请选择相关角色.");
												return;
											}
										
										
									});

					$('#pagination2').jqPaginator(
									{
										totalCounts:totalCount,
										pageSize:pSize,
										visiblePages : pageCount,
										currentPage : 1,
										
										/* first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',*/
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										   /* last: '<li class="last"><a href="javascript:void(0);">Last</a></li>',*/
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
										/*	alert("pageIndex="+num);
										alert("type="+type);*/
											$("#pageIndex").val(num);
											var queryUrl = "queryUeryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#userList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var userInfor = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='userId' class='checkbox' type='checkbox' value='"
																		+ userInfor.id
																		+ "'/></td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.id
																		+ "</td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.loginName
																		+ "</td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.email
																		+ "</td>"
																		+ "<td style='text-align: center;'>";
																if (userInfor.status == 0) {
																	rowContent = rowContent
																			+ "正常";
																} else {
																	rowContent = rowContent
																			+ "<font color='red'>已锁定</font>";
																}

																rowContent = rowContent
																		+ "</td>";
																rowContent = rowContent
																		+ "<td style='text-align: center;'>	<button name='editBtn' objId="+userInfor.id+" class='btn btn-sm btn-danger'>查看/修改权限</button></td>";
																rowContent = rowContent	+ "</tr>";
																$("#userList").append(rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#userList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='userId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#userList").delegate("button[name='editBtn']", 'click', function() {
						var userId = $(this).attr("objId");
						
						var editUrl = "showRole.do?userId=" + userId;
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : userId
							},
							cache : false,

							success : function(data) {
								var userRole=data.userRoles;
								var roleList=data.roles;
								var user=data.user;
								
								loadRole(user,userRole,roleList);
								
							},
							error : function(html) {
								// alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});


					$("#tools")
							.delegate(
									'#deleteBtn',
									'click',
									function() {
										delChk = $('input[name="userId"]:checked');

										if (delChk.length > 0) {
											if (confirm("Are you sure delete the record?")) {

												var ids = "";
												/*var delUrl = "delete.do?ids=";*/
												var delUrl = "delete.do";
                                                var idArr=new Array();
                                               
                                                
                                                for (var i = 0; i < delChk.length; i++) {
                                                	idArr.push(delChk.get(i).value);
                                           
											}
												
												$.ajax({
															url : delUrl,
															type: "POST",
															data : {
																ids : idArr.toString()
															},
															cache : false,

															success : function(
																	html) {
																alert("Delete successfully");
																refresh();
															},
															error : function(
																	html) {
																alert("Delete failly");
																refresh();
															}
														});
											} else {
												return;
											}
										} else {
											alert("Please select the record to delete");
											return;
										}
									});

					
					$("#addRole").delegate($(this),"click",function(){
						if($("#roleList").find("option:selected").length>0){
							var maxId=$('#selectList option').length;
							if(maxId==null || maxId==""|| maxId==undefined){
								maxId=0;
							}
						$("#roleList").find("option:selected").each(function(){
							
							var itemId=$(this).val();
							var itemName=$(this).text();
							var selectOpt="<option value='"+itemId+"'>"+itemName+"</option>";
							$("#selectList").append(selectOpt);
							$("#roleList option[value='"+itemId+"']").remove();
							maxId++;
						});
						}else{
							alert("请选择相关角色.");
						}
				
						
					});
					
					$("#addAll").delegate($(this),"click",function(){
						$("#selectList option").remove();
						var maxId=0;
						if($("#roleList").find("option").length>0){
							
						$("#roleList").find("option").each(function(){
							var itemId=$(this).val();
							var itemName=$(this).text();
							
							var selectOpt="<option value='"+itemId+"'>"+itemName+"</option>";
							$("#selectList").append(selectOpt);
							$("#roleList option[value='"+itemId+"']").remove();
							maxId++;
						});
						}else{
							alert("请选择相关角色.");
						}
				
						
					});
					
					
					
					$("#removeRole").delegate($(this),"click",function(){
						if($("#selectList").find("option:selected").length>0){
						$("#selectList").find("option:selected").each(function(){
							var itemId=$(this).val();
							var itemName=$(this).text();
							var selectOpt="<option value="+itemId+">"+itemName+"</option>";
							$("#roleList").append(selectOpt);
							$("#selectList option[value='"+itemId+"']").remove();
						});
						}else{
							alert("请选择相关角色.");
						}
				
						
					});
					
					$("#removeAll").delegate($(this),"click",function(){
						if($("#selectList").find("option").length>0){
						$("#selectList").find("option").each(function(){
							var itemId=$(this).val();
							var itemName=$(this).text();
							var selectOpt="<option value="+itemId+" >"+itemName+"</option>";
							$("#roleList").append(selectOpt);
							$("#selectList option[value='"+itemId+"']").remove();
						});
						}else{
							alert("请选择相关角色.");
						}
				
						
					});
					


function loadRole(user,userRole,roleList){
	//$("#roleList option").remove();
	$("#loginName").val(user.loginName);
	$("#userId").val(user.id);
	if($("#roleList option").length>0){
		$("#roleList option").remove();
	}
	if($("#selectList option").length>0){
		$("#selectList option").remove();
	}
   
    if(userRole!=null){
	for(k=0;k<userRole.length;k++){
		var urole=userRole[k];
		var opt="<option value="+urole.id+">"+urole.rName+"</option>";
		$("#selectList").append(opt)
	}
    }
	for(j=0;j<roleList.length;j++){
		var role=roleList[j];
		var opt="<option value="+role.id+">"+role.rName+"</option>";
		$("#roleList").append(opt)
	}
	$("#roleDetailDiaglog").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}

				});
